﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework8
{
    public partial class Form1 : Form
    {   //计算器的操作数，成员变量
        public string num1, num2;
        //操作符
        public string opr;
        public Form1()
        {
            InitializeComponent();
        }
        public void NumClick(object sender, EventArgs e)
        {  
            //输入参数转化为button类型
            Button button = (Button)sender;
            //如果还没有输入操作符
            if (string.IsNullOrEmpty(opr))
            {
                num1 = num1 + button.Text;
            }
            else
            {
                num2 = num2 + button.Text;
            }
            //文本框显示
            textResult.Text = textResult.Text + button.Text;
        }
       
       
        public void oprClick(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            //如果还没有输入数字，则不允许按操作符
            if (!String.IsNullOrEmpty(num2))
            {
                MessageBox.Show("此时不应该按入操作符！");
                return;
            }
            opr = button.Text;
            textResult.Text = textResult.Text + button.Text;
        }

        private void textResult_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void butEqual_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(opr)

               || String.IsNullOrEmpty(num1)
               || String.IsNullOrEmpty(num2))
            {
                MessageBox.Show("您输入的内容有误！");
                return;
            }

            //将“=”拼接到框框里
            textResult.Text = textResult.Text + "=";
            //进行两个数的运算
            switch (opr)
            {
                case "+":
                    textResult.Text = textResult.Text + (Int32.Parse(num1) + Int32.Parse(num2));
                    break;
                case "-":
                    textResult.Text = textResult.Text + (Int32.Parse(num1) - Int32.Parse(num2));
                    break;
                case "*":
                    textResult.Text = textResult.Text + (Int32.Parse(num1) * Int32.Parse(num2));
                    break;
                case "/":
                    if (num2 == "0")
                    {
                        MessageBox.Show("除数不可以为零！");
                    }
                    textResult.Text = textResult.Text + (Int32.Parse(num1) / Int32.Parse(num2));
                    break;
            }
            


        }

        private void butClear_Click(object sender, EventArgs e)
        {

            textResult.Text = "";
            num1 = "";
            num2 = "";
            opr = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
